package org.gdstash.item;

import org.gdstash.db.*;
import org.gdstash.description.DetailComposer;
import org.gdstash.description.TagInfo;
import org.gdstash.description.TagInfoHashMap;
import org.gdstash.file.GDReader;
import org.gdstash.file.GDWriter;
import org.gdstash.util.GDColor;
import org.gdstash.util.GDLog;
import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class GDItem implements Cloneable, Comparable<GDItem> {
    public static final int CONTAINER_MULTI = 7;
    private static final byte[] BEGINBLOCK;
    private static final byte[] ENDBLOCK;
    public static final Color COLOR_OVERLAY_COMMON;
    public static final Color COLOR_OVERLAY_MAGICAL;
    public static final Color COLOR_OVERLAY_RARE;
    public static final Color COLOR_OVERLAY_EPIC;
    public static final Color COLOR_OVERLAY_LEGENDARY;
    public static final Color COLOR_OVERLAY_ARTIFACT;
    public static final Color COLOR_OVERLAY_COMPONENT;
    public static final Color COLOR_OVERLAY_ENCHANTMENT;
    public static final Color COLOR_OVERLAY_QUEST;
    public static final Color COLOR_OVERLAY_LORE;
    public static final int CONTAINER_UNDEF = 0;
    public static final int CONTAINER_STASH_SHARED = 1;
    public static final int CONTAINER_STASH_CRAFTING = 2;
    public static final int CONTAINER_STASH_CHAR = 3;
    public static final int CONTAINER_INVENTORY_CHAR = 4;
    public static final int CONTAINER_BAG_CHAR = 5;
    public static final int CONTAINER_EQUIPPED_CHAR = 6;
    private static Random random;
    private InputStream reader;
    private byte[] beginBlock;
    private DBStashItem stashItem;
    private byte[] endBlock;
    private float xPos;
    private int x;
    private float yPos;
    private int y;
    private byte attached;
    private DBItem dbItem;
    private DBAffix dbPrefix;
    private DBAffix dbSuffix;
    private DBAffix dbModifier;
    private DBItem dbComponent;
    private DBAffix dbBonus;
    private DBItem dbEnchantment;
    private List<DBStat> rngStats;
    private int reqDex;
    private int reqInt;
    private int reqStr;
    private boolean error;
    private String filename;
    private int containerType;

    public GDItem(final String charName, final boolean hardcore, final int containerType) {
        this.containerType = containerType;
        this.error = false;
        this.stashItem = new DBStashItem(charName, hardcore);
    }

    public GDItem(final DBItem item) {
        this.containerType = 0;
        this.error = false;
        this.dbItem = item;
        this.stashItem = new DBStashItem(item);
        this.init();
        if (!(this.error = GDMsgLogger.errorsInLog())) {
            this.loadData(0);
        }
    }

    public GDItem(final DBStashItem item) {
        this(item, null);
    }

    public GDItem(final DBStashItem item, final String filename) {
        this.containerType = 0;
        this.error = false;
        this.filename = filename;
        this.stashItem = new DBStashItem(item);
        this.init();
        if (!(this.error = GDMsgLogger.errorsInLog())) {
            this.loadData(0);
        }
    }

    static {
        GDItem.random = new Random();
        BEGINBLOCK = new byte[]{-50, -6, 29, -80};
        ENDBLOCK = new byte[]{-34, -64, -83, -34};
        COLOR_OVERLAY_COMMON = new Color(204, 204, 204);
        COLOR_OVERLAY_MAGICAL = new Color(229, 204, 0);
        COLOR_OVERLAY_RARE = new Color(102, 217, 0);
        COLOR_OVERLAY_EPIC = new Color(76, 115, 217);
        COLOR_OVERLAY_LEGENDARY = new Color(76, 38, 166);
        COLOR_OVERLAY_ARTIFACT = new Color(0, 255, 255);
        COLOR_OVERLAY_COMPONENT = new Color(255, 178, 51);
        COLOR_OVERLAY_ENCHANTMENT = new Color(145, 203, 0);
        COLOR_OVERLAY_QUEST = new Color(204, 51, 229);
        COLOR_OVERLAY_LORE = new Color(194, 176, 196);
    }

    private void init() {
        this.beginBlock = GDItem.BEGINBLOCK;
        this.endBlock = GDItem.ENDBLOCK;
    }

    private void loadData(final int page) {
        if (this.stashItem == null) {
            return;
        }
        if (this.stashItem.getItemID() != null) {
            this.dbItem = this.stashItem.getDBItem();
            if (this.dbItem == null) {
                final Object[] args = {this.filename, page, this.stashItem.getItemID()};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_ITEM_NOT_FOUND", args);
                GDMsgLogger.addWarning(msg);
                this.error = true;
            }
        }
        if (this.stashItem.getStackCount() == 0) {
            this.stashItem.setStackCount(1);
        }
        this.dbPrefix = this.stashItem.getDBPrefix();
        this.dbSuffix = this.stashItem.getDBSuffix();
        this.dbModifier = this.stashItem.getDBModifier();
        this.dbComponent = this.stashItem.getDBComponent();
        this.dbBonus = this.stashItem.getDBCompletionBonus();
        this.dbEnchantment = this.stashItem.getDBAugment();
        this.reqDex = this.stashItem.getRequiredCunning();
        this.reqInt = this.stashItem.getRequiredSpirit();
        this.reqStr = this.stashItem.getRequiredPhysique();
    }

    private void generateRandStats() {
        List<DBStat> list = null;
        if (this.dbItem == null || this.dbItem.getStatList() != null) {
        }
        if (this.dbPrefix == null || this.dbPrefix.getStatList() != null) {
        }
        if (this.dbSuffix != null && this.dbSuffix.getStatList() != null) {
            list = this.cloneStatList(this.dbSuffix.getStatList());
            this.applyRNG(list, this.dbSuffix.getJitterPercent());
        }
        System.out.println();
    }

    private void applyRNG(final List<DBStat> list, final int jitterPercent) {
        final int min = 100 - jitterPercent;
        final int max = 100 + jitterPercent;
        final GDRandomUniform rand = new GDRandomUniform(this.stashItem.getItemSeed());
        rand.generateInt(min, max);
        rand.generateInt(min, max);
        for (final DBStat stat : list) {
            final TagInfo info = TagInfoHashMap.getInfo(stat.getStatType());
            if (info == null) {
                continue;
            }
            if (!info.isJittering()) {
                continue;
            }
            final float factor = rand.generateInt(min, max) / 100.0f;
            stat.setStatMin((float) (int) (stat.getStatMin() * factor));
            stat.setStatMax((float) (int) (stat.getStatMax() * factor));
            stat.setStatModifier((int) (stat.getStatModifier() * factor));
        }
    }

    private List<DBStat> cloneStatList(final List<DBStat> list) {
        if (list == null) {
            return null;
        }
        final List<DBStat> cloneList = new LinkedList<DBStat>();
        for (final DBStat stat : list) {
            final DBStat cloneStat = stat.clone();
            cloneList.add(cloneStat);
        }
        return cloneList;
    }

    public GDItem clone() {
        this.stashItem.fillDependentStats(null);
        final GDItem item = new GDItem(this.stashItem);
        return item;
    }

    @Override
    public int compareTo(final GDItem item) {
        int i = 0;
        if (item.dbItem != null) {
            if (this.dbItem == null) {
                return -1;
            }
            i = this.dbItem.compareTo(item.dbItem);
            if (i != 0) {
                return i;
            }
            int oAffix = 0;
            int iAffix = 0;
            if (this.dbPrefix != null) {
                ++oAffix;
            }
            if (this.dbSuffix != null) {
                oAffix += 2;
            }
            if (item.dbPrefix != null) {
                ++iAffix;
            }
            if (item.dbSuffix != null) {
                iAffix += 2;
            }
            if (iAffix != oAffix) {
                return oAffix - iAffix;
            }
            if (item.dbModifier == null && this.dbModifier != null) {
                return 1;
            }
            if (item.dbModifier != null && this.dbModifier == null) {
                return -1;
            }
            if (item.dbComponent == null && this.dbComponent != null) {
                return 1;
            }
            if (item.dbComponent != null && this.dbComponent == null) {
                return -1;
            }
            if (item.dbEnchantment == null && this.dbEnchantment != null) {
                return 1;
            }
            if (item.dbEnchantment != null && this.dbEnchantment == null) {
                return -1;
            }
            final int oLevel = this.getRequiredLevel();
            final int iLevel = item.getRequiredLevel();
            if (oLevel < iLevel) {
                return -1;
            }
            if (oLevel > iLevel) {
                return 1;
            }
            if (item.dbPrefix != null) {
                if (this.dbPrefix == null) {
                    throw new AssertionError((Object) GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_PREFIX_BOTH"));
                }
                i = this.dbPrefix.compareTo(item.dbPrefix);
                if (i != 0) {
                    return i;
                }
            } else if (this.dbPrefix != null) {
                throw new AssertionError((Object) GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_PREFIX_NEITHER"));
            }
            if (item.dbSuffix != null) {
                if (this.dbSuffix == null) {
                    throw new AssertionError((Object) GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_SUFFIX_BOTH"));
                }
                i = this.dbSuffix.compareTo(item.dbSuffix);
                if (i != 0) {
                    return i;
                }
            } else if (this.dbSuffix != null) {
                throw new AssertionError((Object) GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_SUFFIX_NEITHER"));
            }
            if (item.dbModifier != null) {
                if (this.dbModifier == null) {
                    throw new AssertionError((Object) GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_MODIFIER_BOTH"));
                }
                i = this.dbModifier.compareTo(item.dbModifier);
                if (i != 0) {
                    return i;
                }
            } else if (this.dbModifier != null) {
                throw new AssertionError((Object) GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_MODIFIER_NEITHER"));
            }
            if (item.dbComponent != null) {
                if (this.dbComponent == null) {
                    throw new AssertionError((Object) GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_COMPONENT_BOTH"));
                }
                i = this.dbComponent.compareTo(item.dbComponent);
                if (i != 0) {
                    return i;
                }
                if (item.stashItem.getCompletionBonusID() != null) {
                    if (this.stashItem.getCompletionBonusID() == null) {
                        return -1;
                    }
                    if (!item.stashItem.getCompletionBonusID().equals(this.stashItem.getCompletionBonusID())) {
                        i = this.stashItem.getCompletionBonusID().compareTo(item.stashItem.getCompletionBonusID());
                        if (i != 0) {
                            return i;
                        }
                    }
                } else if (this.stashItem.getCompletionBonusID() != null) {
                    return 1;
                }
            } else if (this.dbComponent != null) {
                throw new AssertionError((Object) GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_COMPONENT_NEITHER"));
            }
            if (item.dbEnchantment != null) {
                if (this.dbEnchantment == null) {
                    throw new AssertionError((Object) GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_ENCHANTMENT_BOTH"));
                }
                i = this.dbEnchantment.compareTo(item.dbEnchantment);
                if (i != 0) {
                    return i;
                }
            } else if (this.dbEnchantment != null) {
                throw new AssertionError((Object) GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_ENCHANTMENTENT_NEITHER"));
            }
            if (this.stashItem.getStashID() < item.stashItem.getStashID()) {
                return -1;
            }
            if (this.stashItem.getStashID() > item.stashItem.getStashID()) {
                return 1;
            }
            return 0;
        } else {
            if (this.dbItem != null) {
                return 1;
            }
            return 0;
        }
    }

    public boolean hasErrors() {
        return this.error;
    }

    public int getContainerType() {
        return this.containerType;
    }

    public DBItem getDBItem() {
        return this.dbItem;
    }

    public DBStashItem getDBStashItem() {
        return this.stashItem;
    }

    public DBItem getDBComponent() {
        return this.dbComponent;
    }

    public DBAffixSet getBonusAffixSet() {
        if (this.dbItem == null) {
            return null;
        }
        return this.dbItem.getBonusAffixSet();
    }

    public GDItem(final InputStream reader, final int stashType, final int containerVersion, final boolean hardcore, final String filename, final int page, final GDLog log) {
        this.containerType = 1;
        this.error = false;
        this.filename = filename;
        this.stashItem = new DBStashItem();
        this.reader = reader;
        this.stashItem.setHardcore(hardcore);
        this.stashItem.setCharName(null);
        final GDLog tLog = new GDLog();
        if (stashType == 0) {
            this.readOldFormat(tLog);
        }
        if (stashType == 1) {
            this.readNewFormat(containerVersion, tLog);
        }
        if (log == null) {
            GDMsgLogger.addLog(tLog);
        } else {
            log.addLog(tLog);
        }
        if (!this.error) {
            this.loadData(page);
        }
    }

    public DBStashItem getStashItem() {
        return this.stashItem;
    }

    public String getItemID() {
        if (this.stashItem == null) {
            return null;
        }
        return this.stashItem.getItemID();
    }

    public String getItemClass() {
        if (this.dbItem == null) {
            return null;
        }
        return this.dbItem.getItemClass();
    }

    public String getRarity() {
        if (this.stashItem != null) {
            return this.stashItem.getRarity();
        }
        if (this.dbItem != null) {
            return this.dbItem.getRarity();
        }
        return "Common";
    }

    public String getBaseName() {
        if (this.dbItem == null) {
            return null;
        }
        return this.dbItem.getName(true);
    }

    public String getShortItemName() {
        if (this.dbItem == null) {
            return null;
        }
        return this.dbItem.getName(false);
    }

    public String getLongItemName() {
        if (this.dbItem == null) {
            return null;
        }
        return this.dbItem.getFullName();
    }

    public String getComponentName() {
        if (this.dbComponent == null) {
            return null;
        }
        String s = this.dbComponent.getName(false);
        if (this.stashItem.getVar1() < this.dbComponent.getComponentPieces()) {
            s = s + " (" + Integer.toString(this.stashItem.getVar1() + 1) + "/" + Integer.toString(this.dbComponent.getComponentPieces()) + ")";
        }
        return s;
    }

    public String getEnchantmentName() {
        if (this.dbEnchantment == null) {
            return null;
        }
        return this.dbEnchantment.getName(true);
    }

    public List<DBStat> getStatList() {
        final List<DBStat> listAll = new LinkedList<DBStat>();
        List<DBStat> list = null;
        if (this.dbItem != null) {
            list = this.dbItem.getStatList();
            if (list != null) {
                listAll.addAll(list);
            }
        }
        if (this.dbPrefix != null) {
            list = this.dbPrefix.getStatList();
            if (list != null) {
                listAll.addAll(list);
            }
        }
        if (this.dbSuffix != null) {
            list = this.dbSuffix.getStatList();
            if (list != null) {
                listAll.addAll(list);
            }
        }
        return listAll;
    }

    public boolean hasPetBonus() {
        if (this.dbItem != null && this.dbItem.getPetBonusSkill() != null) {
            return true;
        }
        if (this.dbPrefix != null) {
            if (this.dbPrefix.getPetAffix() != null) {
                return true;
            }
            if (this.dbPrefix.getPetSkill() != null) {
                return true;
            }
        }
        if (this.dbSuffix != null) {
            if (this.dbSuffix.getPetAffix() != null) {
                return true;
            }
            if (this.dbSuffix.getPetSkill() != null) {
                return true;
            }
        }
        return false;
    }

    public boolean hasConvertIn(final String convertIn) {
        return (this.dbItem != null && convertIn.equals(this.dbItem.getConvertIn())) || (this.dbPrefix != null && convertIn.equals(this.dbPrefix.getConvertIn())) || (this.dbSuffix != null && convertIn.equals(this.dbSuffix.getConvertIn()));
    }

    public boolean hasConvertOut(final String convertOut) {
        return (this.dbItem != null && convertOut.equals(this.dbItem.getConvertOut())) || (this.dbPrefix != null && convertOut.equals(this.dbPrefix.getConvertOut())) || (this.dbSuffix != null && convertOut.equals(this.dbSuffix.getConvertOut()));
    }

    public boolean isSoulbound() {
        return this.stashItem != null && this.stashItem.isSoulbound();
    }

    public boolean isCraftable() {
        return this.dbItem != null && this.dbItem.isCraftable();
    }

    public boolean isHardcore() {
        return this.stashItem != null && this.stashItem.isHardcore();
    }

    public boolean isArmor() {
        return this.dbItem != null && this.dbItem.isArmor();
    }

    public boolean isJewelry() {
        return this.dbItem != null && this.dbItem.isJewelry();
    }

    public boolean isWeapon() {
        return this.dbItem != null && this.dbItem.isWeapon();
    }

    public boolean isOffhand() {
        return this.dbItem != null && this.dbItem.isOffhand();
    }

    public boolean is1HWeapon() {
        return this.dbItem != null && this.dbItem.is1HWeapon();
    }

    public boolean is2HWeapon() {
        return this.dbItem != null && this.dbItem.is2HWeapon();
    }

    public boolean isArtifact() {
        return this.dbItem != null && this.dbItem.isArtifact();
    }

    public boolean isComponent() {
        return this.dbItem != null && this.dbItem.isComponent();
    }

    public boolean isEnchantment() {
        return this.dbItem != null && this.dbItem.isEnchantment();
    }

    public boolean isNote() {
        return this.dbItem != null && this.dbItem.isNote();
    }

    public boolean isFactionBooster() {
        return this.dbItem != null && this.dbItem.isFactionBooster();
    }

    public boolean isElixir() {
        return this.dbItem != null && this.dbItem.isElixir();
    }

    public boolean isPotion() {
        return this.dbItem != null && this.dbItem.isPotion();
    }

    public boolean isQuestItem() {
        return this.dbItem != null && this.dbItem.isQuestItem();
    }

    public boolean isStackable() {
        return this.stashItem != null && this.stashItem.isStackable();
    }

    public boolean isEpic() {
        return this.stashItem != null && this.stashItem.isEpic();
    }

    public boolean isLegendary() {
        return this.stashItem != null && this.stashItem.isLegendary();
    }

    public boolean isUnique() {
        return this.stashItem != null && this.stashItem.isUnique();
    }

    public int getDefaultStackSize() {
        if (this.stashItem == null) {
            return 1;
        }
        return this.stashItem.getDefaultStackSize();
    }

    public String getPrefixID() {
        if (this.dbPrefix == null) {
            return null;
        }
        return this.dbPrefix.getAffixID();
    }

    public DBAffix getPrefix() {
        return this.dbPrefix;
    }

    public String getSuffixID() {
        if (this.dbSuffix == null) {
            return null;
        }
        return this.dbSuffix.getAffixID();
    }

    public DBAffix getSuffix() {
        return this.dbSuffix;
    }

    public DBAffix getModifier() {
        return this.dbModifier;
    }

    public DBAffix getCompletionBonus() {
        return this.dbBonus;
    }

    public DBItem getAugment() {
        return this.dbEnchantment;
    }

    public String getSeedHex() {
        if (this.stashItem == null) {
            return null;
        }
        final byte[] seed = GDWriter.intToBytes4(this.stashItem.getItemSeed());
        String s = "";
        for (int i = 0; i < seed.length; ++i) {
            final byte val = seed[seed.length - 1 - i];
            s += byteToHex(val);
        }
        return s;
    }

    public int getVar1() {
        if (this.stashItem == null) {
            return 0;
        }
        return this.stashItem.getVar1();
    }

    public String getVar1Str() {
        if (this.stashItem == null) {
            return null;
        }
        int i = this.getVar1();
        if (i == 0) {
            i = 1;
        }
        return Integer.toString(i);
    }

    public int getStackCount() {
        if (this.stashItem == null) {
            return 0;
        }
        return this.stashItem.getStackCount();
    }

    public String getStackCountStr() {
        if (this.stashItem == null) {
            return null;
        }
        return Integer.toString(this.getStackCount());
    }

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

    public String getCharName() {
        if (this.stashItem == null) {
            return null;
        }
        return this.stashItem.getCharName();
    }

    public int getStashID() {
        if (this.stashItem == null) {
            return -1;
        }
        return this.stashItem.getStashID();
    }

    private static String byteToHex(final byte b) {
        int i = b;
        if (i < 0) {
            i += 256;
        }
        final int v1 = i / 16;
        final int v2 = i % 16;
        final String s = intDigitToHex(v1) + intDigitToHex(v2);
        return s;
    }

    public int getItemSkillLevel() {
        if (this.stashItem == null) {
            return 1;
        }
        return this.stashItem.getItemSkillLevel();
    }

    private static String intDigitToHex(final int val) {
        String s = null;
        switch (val) {
            case 10: {
                s = "A";
                break;
            }
            case 11: {
                s = "B";
                break;
            }
            case 12: {
                s = "C";
                break;
            }
            case 13: {
                s = "D";
                break;
            }
            case 14: {
                s = "E";
                break;
            }
            case 15: {
                s = "F";
                break;
            }
            default: {
                if (val >= 0 && val <= 9) {
                    s = Integer.toString(val);
                    break;
                }
                break;
            }
        }
        return s;
    }

    public int getRequiredCunning() {
        return this.reqDex;
    }

    public int getRequiredPhysique() {
        return this.reqStr;
    }

    public int getRequiredSpirit() {
        return this.reqInt;
    }

    public Color getOverlayColor() {
        if (this.dbItem == null) {
            return null;
        }
        Color color = GDItem.COLOR_OVERLAY_COMMON;
        final String rarity = this.getRarity();
        if (rarity == null) {
            return color;
        }
        if (rarity.equals("Magical")) {
            color = GDItem.COLOR_OVERLAY_MAGICAL;
        }
        if (rarity.equals("Rare")) {
            color = GDItem.COLOR_OVERLAY_RARE;
        }
        if (rarity.equals("Epic")) {
            color = GDItem.COLOR_OVERLAY_EPIC;
        }
        if (rarity.equals("Legendary")) {
            color = GDItem.COLOR_OVERLAY_LEGENDARY;
        }
        if (this.dbItem.isArtifact()) {
            color = GDItem.COLOR_OVERLAY_ARTIFACT;
        }
        if (this.dbItem.isComponent()) {
            color = GDItem.COLOR_OVERLAY_COMPONENT;
        }
        if (this.dbItem.isEnchantment()) {
            color = GDItem.COLOR_OVERLAY_ENCHANTMENT;
        }
        if (this.dbItem.isQuestItem()) {
            color = GDItem.COLOR_OVERLAY_QUEST;
        }
        return color;
    }

    public BufferedImage getFullImage() {
        if (this.dbItem == null) {
            return null;
        }
        BufferedImage img = null;
        if (this.dbItem.getComponentPieces() > 0 && this.getVar1() < this.dbItem.getComponentPieces()) {
            img = this.dbItem.getShardImage();
        } else {
            img = this.dbItem.getImage();
        }
        if (img == null) {
            return null;
        }
        final BufferedImage clone = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
        final Graphics2D g = clone.createGraphics();
        g.drawImage(img, 0, 0, null);
        img = clone;
        final BufferedImage overlay = this.getOverlayImage();
        if (overlay != null) {
            final int w = img.getWidth();
            final int h = img.getHeight();
            final int wo = overlay.getWidth();
            final int ho = overlay.getHeight();
            g.drawImage(overlay, w - wo - 2, h - ho - 2, null);
        }
        if (this.dbItem.getComponentPieces() > 0 && this.getVar1() < this.dbItem.getComponentPieces()) {
            final String s = this.getVar1Str();
            Font font = UIManager.getDefaults().getFont("Label.font");
            font = font.deriveFont(1, 10.0f);
            g.setFont(font);
            final int h2 = g.getFontMetrics().getAscent();
            final int xPos = 4;
            final int yPos = h2 + 2;
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos - 1, yPos - 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos - 1, yPos + 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos + 1, yPos - 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos + 1, yPos + 1);
            g.setPaint(Color.WHITE);
            g.drawString(s, xPos, yPos);
        }
        if (this.getStackCount() > 1) {
            final String s = this.getStackCountStr();
            Font font = UIManager.getDefaults().getFont("Label.font");
            font = font.deriveFont(1, 10.0f);
            g.setFont(font);
            final int w2 = g.getFontMetrics().stringWidth(s);
            final int h3 = g.getFontMetrics().getDescent();
            final int xPos2 = img.getWidth() - w2 - 4;
            final int yPos2 = img.getHeight() - h3 - 2;
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos2 - 1, yPos2 - 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos2 - 1, yPos2 + 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos2 + 1, yPos2 - 1);
            g.setPaint(Color.BLACK);
            g.drawString(s, xPos2 + 1, yPos2 + 1);
            g.setPaint(Color.WHITE);
            g.drawString(s, xPos2, yPos2);
        }
        return img;
    }

    public BufferedImage getImage() {
        if (this.dbItem == null) {
            return null;
        }
        return this.dbItem.getImage();
    }

    public BufferedImage getOverlayImage() {
        if (this.dbComponent == null) {
            return null;
        }
        BufferedImage img = null;
        if (this.dbComponent.getComponentPieces() > 0 && this.getVar1() < this.dbComponent.getComponentPieces()) {
            img = this.dbComponent.getOverlayShard();
        } else {
            img = this.dbComponent.getOverlayImage();
        }
        return img;
    }

    public ImageIcon getImageIcon() {
        ImageIcon icon = null;
        final BufferedImage image = this.getFullImage();
        if (image != null) {
            icon = new ImageIcon(image);
        }
        return icon;
    }

    public int getPlusAllSkills() {
        if (this.dbItem == null) {
            return 0;
        }
        return this.dbItem.getPlusAllSkills();
    }

    public String getMainDamageType() {
        if (this.dbItem == null) {
            return null;
        }
        return this.dbItem.getMainDamageType();
    }

    public String getFullDescription() {
        if (this.dbItem == null) {
            return null;
        }
        final ItemInfo info = this.compactBonuses();
        return DetailComposer.getItemText(this, info);
    }

    public String getItemDescription() {
        if (this.dbItem == null) {
            return null;
        }
        return this.dbItem.getItemDescription();
    }

    private String getSlots() {
        if (this.dbItem == null) {
            return "";
        }
        if (!this.dbItem.usesSlots()) {
            return "";
        }
        final ItemSlots slots = this.dbItem.getSlots();
        String allSlots = null;
        String singleSlot = null;
        if (slots.usesAllArmor()) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_ALL", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
            slots.clearAllArmor();
        }
        if (slots.usesAllWeapons()) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_ALL", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
            slots.clearAllWeapons();
        }
        if (slots.usesMeleeWeapons()) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_MELEE", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
            slots.clearMeleeWeapons();
        }
        if (slots.uses1HWeapons()) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_ONE_HAND", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
            slots.clear1HWeapons();
        }
        if (slots.uses2HWeapons()) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_TWO_HAND", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
            slots.clear2HWeapons();
        }
        if (slots.usesRangedWeapons()) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_RANGED", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
            slots.clearRangedWeapons();
        }
        if (slots.slotAxe1H) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_AXE", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotDagger1H) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_DAGGER", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotMace1H) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_MACE", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotScepter1H) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_SCEPTER", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotSpear1H) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_SPEAR", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotStaff2H) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_STAFF", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotSword1H) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_SWORD", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotShield) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_SHIELD", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotOffhand) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_WEAPON_OFFHAND", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotHead) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_HEAD", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotShoulders) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_SHOULDERS", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotChest) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_CHEST", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotHands) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_HANDS", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotLegs) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_LEGS", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotFeet) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_FEET", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotBelt) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_BELT", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotRing) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_RING", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotAmulet) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_AMULET", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (slots.slotMedal) {
            singleSlot = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SLOT_ARMOR_MEDAL", null);
            if (allSlots == null) {
                allSlots = singleSlot;
            } else {
                allSlots = allSlots + ", " + singleSlot;
            }
        }
        if (allSlots == null) {
            return "";
        }
        final Object[] args = {allSlots};
        allSlots = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_USED_IN", args);
        allSlots = "<br>" + GDColor.HTML_COLOR_USED_SLOTS + allSlots + "</font>";
        return allSlots;
    }

    public String getItemCategory() {
        final String tagRarity = this.determineRarityTag();
        final String tagCategory = this.determineCategoryTag();
        String tagItemType = this.determineItemTypeTag();
        if (tagItemType == null) {
            return null;
        }
        if (tagItemType.equals("TYPE_ITEM_COMPONENT") && this.stashItem.getVar1() < this.dbItem.getComponentPieces()) {
            tagItemType = "TYPE_ITEM_COMPONENT_PARTIAL";
        }
        String strRarity = null;
        String strCategory = null;
        String strItemType = GDMsgFormatter.format(GDMsgFormatter.rbGD, tagItemType, null);
        if (tagCategory != null) {
            strCategory = GDMsgFormatter.format(GDMsgFormatter.rbGD, tagCategory, null);
        }
        if (tagRarity != null) {
            strRarity = GDMsgFormatter.format(GDMsgFormatter.rbGD, tagRarity, null);
        }
        if (strRarity != null && strCategory != null) {
            final Object[] args = {strRarity, strCategory, strItemType};
            strItemType = GDMsgFormatter.format(GDMsgFormatter.rbGD, "COMB_RARITY_CATEGORY_ITEM", args);
        }
        if (strRarity == null && strCategory != null) {
            final Object[] args = {strCategory, strItemType};
            strItemType = GDMsgFormatter.format(GDMsgFormatter.rbGD, "COMB_CATEGORY_ITEM", args);
        }
        if (strRarity != null && strCategory == null) {
            final Object[] args = {strRarity, strItemType};
            strItemType = GDMsgFormatter.format(GDMsgFormatter.rbGD, "COMB_RARITY_ITEM", args);
        }
        if (this.isSoulbound()) {
            final Object[] args = {strItemType};
            strItemType = GDMsgFormatter.format(GDMsgFormatter.rbGD, "COMB_SOULBOUND_ITEM", args);
        }
        if (this.getItemSetName() != null) {
            strItemType = strItemType + "<br>" + "[" + this.getItemSetName() + "]";
        }
        strItemType = GDColor.HTML_COLOR_ITEM_TYPE + strItemType + "</font>";
        final String slots = this.getSlots();
        if (slots != null) {
            strItemType += slots;
        }
        return strItemType;
    }

    private Color getBaseItemColor() {
        Color color = GDColor.COLOR_FG_COMMON;
        if (this.dbItem != null) {
            final String rarity = this.dbItem.getRarity();
            if (rarity != null) {
                color = ItemClass.getRarityColor(rarity);
            }
            if (this.dbItem.isQuestItem()) {
                color = GDColor.COLOR_FG_LEGENDARY;
            }
        }
        return color;
    }

    private Color getRarityBackgroundColor() {
        final String rarity = this.getRarity();
        Color color = GDColor.COLOR_BG_COMMON;
        if (rarity != null) {
            color = ItemClass.getRarityBackgroundColor(rarity);
        }
        if (this.dbItem != null && this.dbItem.isQuestItem()) {
            color = GDColor.COLOR_BG_LEGENDARY;
        }
        return color;
    }

    private Color getItemColor() {
        Color color = GDColor.COLOR_FG_COMMON;
        final String rarity = this.getRarity();
        if (rarity != null) {
            color = ItemClass.getRarityColor(rarity);
        }
        if (this.dbItem != null && this.dbItem.isQuestItem()) {
            color = GDColor.COLOR_FG_LEGENDARY;
        }
        return color;
    }

    public boolean isValid(final int affixCombi, final boolean completionAll) {
        if (this.dbItem == null) {
            return false;
        }
        if (affixCombi == 3) {
            return true;
        }
        final boolean validCombo = this.isValidAffixCombo(affixCombi);
        final boolean validComponent = this.isValidComponent();
        final boolean validEnchantment = this.isValidEnchantment();
        final boolean validBonus = this.isValidBonus(completionAll);
        final boolean valid = validCombo && validComponent && validEnchantment && validBonus;
        return valid;
    }

    private boolean isValidAffixCombo(final int affixCombi) {
        if (this.dbItem == null) {
            return false;
        }
        if (affixCombi == 3) {
            return true;
        }
        if (this.dbPrefix == null && this.dbSuffix == null) {
            return true;
        }
        final List<DBLootTable> tables = DBLootTable.getByItemID(this.dbItem.getItemID());
        if (tables == null) {
            return this.dbPrefix == null && this.dbSuffix == null;
        }
        boolean validCombo = false;
        for (final DBLootTable table : tables) {
            boolean foundPrefix = this.dbPrefix == null;
            int rarePrefix = 6;
            boolean foundSuffix = this.dbSuffix == null;
            int rareSuffix = 6;
            final List<DBLootTableAffixSetAlloc> affixSets = table.getAffixSetAllocList();
            if (affixSets != null) {
                for (final DBLootTableAffixSetAlloc alloc : affixSets) {
                    final int type = alloc.getAffixType();
                    final List<DBAffixSet.DBEntry> list = alloc.getAffixEntries();
                    if (list == null) {
                        continue;
                    }
                    if ((type == 1 || type == 3) && this.dbPrefix != null) {
                        for (final DBAffixSet.DBEntry entry : list) {
                            if (entry.getAffixID().equals(this.dbPrefix.getAffixID())) {
                                foundPrefix = true;
                                if (type == 1 && rarePrefix > 3) {
                                    rarePrefix = 3;
                                }
                                if (type == 3 && rarePrefix > 4) {
                                    rarePrefix = 4;
                                    break;
                                }
                                break;
                            }
                        }
                    }
                    if ((type != 2 && type != 4) || this.dbSuffix == null) {
                        continue;
                    }
                    for (final DBAffixSet.DBEntry entry : list) {
                        if (entry.getAffixID().equals(this.dbSuffix.getAffixID())) {
                            foundSuffix = true;
                            if (type == 2 && rareSuffix > 3) {
                                rareSuffix = 3;
                            }
                            if (type == 4 && rareSuffix > 4) {
                                rareSuffix = 4;
                                break;
                            }
                            break;
                        }
                    }
                }
                if (affixCombi == 2 && foundPrefix && foundSuffix) {
                    validCombo = true;
                }
                if (affixCombi == 1) {
                    if (this.dbPrefix != null && this.dbSuffix != null) {
                        if (foundPrefix && foundSuffix) {
                            if (rarePrefix == 3 && rareSuffix == 3) {
                                validCombo = table.isMagicPrefixMagicSuffixAllowed();
                            }
                            if (rarePrefix == 3 && rareSuffix == 4) {
                                validCombo = table.isMagicPrefixRareSuffixAllowed();
                            }
                            if (rarePrefix == 4 && rareSuffix == 3) {
                                validCombo = table.isRarePrefixMagicSuffixAllowed();
                            }
                            if (rarePrefix == 4 && rareSuffix == 4) {
                                validCombo = table.isRarePrefixRareSuffixAllowed();
                            }
                        }
                    } else if (foundPrefix && foundSuffix) {
                        validCombo = true;
                    }
                }
            }
            if (validCombo) {
                break;
            }
        }
        return validCombo;
    }

    private static byte hexDigitToByte(final char c) {
        byte b = -1;
        switch (c) {
            case '0': {
                b = 0;
                break;
            }
            case '1': {
                b = 1;
                break;
            }
            case '2': {
                b = 2;
                break;
            }
            case '3': {
                b = 3;
                break;
            }
            case '4': {
                b = 4;
                break;
            }
            case '5': {
                b = 5;
                break;
            }
            case '6': {
                b = 6;
                break;
            }
            case '7': {
                b = 7;
                break;
            }
            case '8': {
                b = 8;
                break;
            }
            case '9': {
                b = 9;
                break;
            }
            case 'A': {
                b = 10;
                break;
            }
            case 'B': {
                b = 11;
                break;
            }
            case 'C': {
                b = 12;
                break;
            }
            case 'D': {
                b = 13;
                break;
            }
            case 'E': {
                b = 14;
                break;
            }
            case 'F': {
                b = 15;
                break;
            }
        }
        return b;
    }

    private boolean isValidComponent() {
        return this.isValidSlotItem(this.dbComponent);
    }

    private boolean isValidEnchantment() {
        return this.isValidSlotItem(this.dbEnchantment);
    }

    private boolean isValidBonus(final boolean completionAll) {
        if (this.dbItem == null) {
            return false;
        }
        if (this.dbBonus == null) {
            return true;
        }
        if (this.dbComponent == null && !this.isComponent() && !this.isArtifact()) {
            return false;
        }
        if (completionAll) {
            return true;
        }
        DBItem component = null;
        if (this.isComponent() || this.isArtifact()) {
            component = this.dbItem;
        } else {
            component = this.dbComponent;
        }
        if (component.getBonusAffixSet() == null) {
            return false;
        }
        final List<DBAffixSet.DBEntry> entries = component.getBonusAffixSet().getAffixEntries();
        if (entries == null) {
            return false;
        }
        boolean found = false;
        for (final DBAffixSet.DBEntry entry : entries) {
            if (entry.getAffixID().equals(this.dbBonus.getAffixID())) {
                found = true;
                break;
            }
        }
        return found;
    }

    public LabelInfo getItemNameInfo() {
        if (this.dbItem == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        boolean brackets = false;
        li.text = this.parseName();
        if (this.dbItem.getRequiredlevel() != 0) {
            li.text = li.text + " [" + this.dbItem.getRequiredlevel();
            brackets = true;
        }
        final String dmg = this.dbItem.getMainDamageType();
        if (dmg != null) {
            if (brackets) {
                li.text = li.text + ", " + dmg;
            } else {
                li.text = li.text + " [" + dmg;
            }
            brackets = true;
        }
        if (brackets) {
            li.text += "]";
        }
        li.foreground = this.getBaseItemColor();
        return li;
    }

    public LabelInfo getCompleteNameInfo(final boolean inclFaction, final boolean inclMod) {
        if (this.stashItem == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        li.foreground = this.getItemColor();
        li.background = this.getRarityBackgroundColor();
        li.text = this.stashItem.getCompleteName(inclFaction, inclMod);
        return li;
    }

    public LabelInfo getPrefixInfo() {
        if (this.dbPrefix == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        li.text = this.dbPrefix.getGenderText(this.dbItem.getGenderCode());
        if (this.dbPrefix.getRequiredlevel() != 0) {
            li.text = li.text + " [" + this.dbPrefix.getRequiredlevel() + "]";
        }
        final String rarity = this.dbPrefix.getRarity();
        if (rarity != null) {
            if (rarity.equals("Magical")) {
                li.foreground = GDColor.COLOR_FG_MAGICAL;
            }
            if (rarity.equals("Rare")) {
                li.foreground = GDColor.COLOR_FG_RARE;
            }
        }
        return li;
    }

    public LabelInfo getSuffixInfo() {
        if (this.dbSuffix == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        li.text = this.dbSuffix.getGenderText(this.dbItem.getGenderCode());
        if (this.dbSuffix.getRequiredlevel() != 0) {
            li.text = li.text + " [" + this.dbSuffix.getRequiredlevel() + "]";
        }
        final String rarity = this.dbSuffix.getRarity();
        if (rarity != null) {
            if (rarity.equals("Magical")) {
                li.foreground = GDColor.COLOR_FG_MAGICAL;
            }
            if (rarity.equals("Rare")) {
                li.foreground = GDColor.COLOR_FG_RARE;
            }
        }
        return li;
    }

    public LabelInfo getModifierInfo() {
        if (this.dbModifier == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        li.text = this.dbModifier.getGenderText(this.dbItem.getGenderCode());
        return li;
    }

    public LabelInfo getComponentInfo() {
        if (this.dbComponent == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        li.text = this.dbComponent.getName(false);
        li.foreground = GDColor.COLOR_FG_COMPONENT;
        return li;
    }

    public LabelInfo getComponentBonusInfo() {
        if (this.dbBonus == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        final DetailComposer compCompletion = DetailComposer.createComposer(this.stashItem, 1, this.dbBonus.getStatList(), this.dbBonus.getSkillBonusList());
        String s = DetailComposer.getComposerBonuses(compCompletion, true, true, true);
        if (s != null) {
            s = "<html>" + s + "</html>";
        }
        li.text = s;
        li.foreground = GDColor.COLOR_FG_COMPONENT;
        return li;
    }

    public LabelInfo getEnchantmentInfo() {
        if (this.dbEnchantment == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        li.text = this.dbEnchantment.getItemName();
        li.foreground = GDColor.COLOR_FG_ENCHANTMENT;
        return li;
    }

    public LabelInfo getSeedHexInfo() {
        if (this.stashItem == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        li.text = this.getSeedHex();
        return li;
    }

    public String getItemSetID() {
        if (this.dbItem == null) {
            return null;
        }
        return this.dbItem.getItemSetID();
    }

    public String getItemSetName() {
        if (this.dbItem == null) {
            return null;
        }
        return this.dbItem.getItemSetName();
    }

    public LabelInfo getItemSetNameInfo() {
        if (this.dbItem == null) {
            return null;
        }
        if (this.dbItem.getItemSetName() == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        li.text = this.dbItem.getItemSetName();
        return li;
    }

    public LabelInfo getRequiredLevelInfo() {
        if (this.dbItem == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        li.text = Integer.toString(this.getRequiredLevel());
        return li;
    }

    public LabelInfo getCharNameInfo() {
        if (this.stashItem == null) {
            return null;
        }
        final LabelInfo li = new LabelInfo();
        li.text = this.getCharName();
        if (li.text == null) {
            li.text = "";
        }
        return li;
    }

    public static int generateSeed() {
        return GDItem.random.nextInt();
    }

    public List<DBAffix> getBonusAffixList() {
        final List<DBAffix> list = new LinkedList<DBAffix>();
        if (this.dbItem == null) {
            return list;
        }
        return this.dbItem.getBonusAffixList();
    }

    public int getItemLevel() {
        int level = 0;
        if (this.stashItem != null) {
            level = this.stashItem.getItemLevel();
        } else if (this.dbItem != null) {
            level = this.dbItem.getItemLevel();
        }
        return level;
    }

    public int getRequiredLevel() {
        int level = 0;
        if (this.stashItem != null) {
            level = this.stashItem.getRequiredlevel();
        } else if (this.dbItem != null) {
            level = this.dbItem.getRequiredlevel();
        }
        return level;
    }

    private boolean isValidSlotItem(final DBItem item) {
        if (this.dbItem == null) {
            return false;
        }
        if (item == null) {
            return true;
        }
        boolean valid = false;
        final String itemClass = this.dbItem.getItemClass();
        if (itemClass.equals("ArmorProtective_Head")) {
            valid = item.isSlotHead();
        }
        if (itemClass.equals("ArmorProtective_Shoulders")) {
            valid = item.isSlotShoulders();
        }
        if (itemClass.equals("ArmorProtective_Chest")) {
            valid = item.isSlotChest();
        }
        if (itemClass.equals("ArmorProtective_Hands")) {
            valid = item.isSlotHands();
        }
        if (itemClass.equals("ArmorProtective_Waist")) {
            valid = item.isSlotBelt();
        }
        if (itemClass.equals("ArmorProtective_Legs")) {
            valid = item.isSlotLegs();
        }
        if (itemClass.equals("ArmorProtective_Feet")) {
            valid = item.isSlotFeet();
        }
        if (itemClass.equals("ArmorJewelry_Amulet")) {
            valid = item.isSlotAmulet();
        }
        if (itemClass.equals("ArmorJewelry_Medal")) {
            valid = item.isSlotMedal();
        }
        if (itemClass.equals("ArmorJewelry_Ring")) {
            valid = item.isSlotRing();
        }
        if (itemClass.equals("WeaponArmor_Offhand")) {
            valid = item.isSlotOffhand();
        }
        if (itemClass.equals("WeaponArmor_Shield")) {
            valid = item.isSlotShield();
        }
        if (itemClass.equals("WeaponMelee_Axe")) {
            valid = item.isSlotAxe1H();
        }
        if (itemClass.equals("WeaponMelee_Mace")) {
            valid = item.isSlotMace1H();
        }
        if (itemClass.equals("WeaponHunting_Spear")) {
            valid = item.isSlotSpear1H();
        }
        if (itemClass.equals("WeaponMelee_Sword")) {
            valid = item.isSlotSword1H();
        }
        if (itemClass.equals("WeaponMelee_Dagger")) {
            valid = item.isSlotDagger1H();
        }
        if (itemClass.equals("WeaponMelee_Scepter")) {
            valid = item.isSlotScepter1H();
        }
        if (itemClass.equals("WeaponHunting_Ranged1h")) {
            valid = item.isSlotRanged1H();
        }
        if (itemClass.equals("WeaponMelee_Axe2h")) {
            valid = item.isSlotAxe2H();
        }
        if (itemClass.equals("WeaponMelee_Mace2h")) {
            valid = item.isSlotMace2H();
        }
        if (itemClass.equals("WeaponMelee_Spear2h")) {
            valid = item.isSlotSpear2H();
        }
        if (itemClass.equals("WeaponMagical_Staff")) {
            valid = item.isSlotStaff2H();
        }
        if (itemClass.equals("WeaponMelee_Sword2h")) {
            valid = item.isSlotSword2H();
        }
        if (itemClass.equals("WeaponHunting_Ranged2h")) {
            valid = item.isSlotRanged2H();
        }
        return valid;
    }

    public void setContainerType(int containerType) {
        this.containerType = containerType;
        if (containerType > 6) {
            containerType = 0;
        }
        if (containerType == 6) {
            this.attached = 1;
        }
    }

    public void setPrefix(final DBAffix affix) {
        this.dbPrefix = affix;
        if (affix == null) {
            this.stashItem.setPrefixID(null);
        } else {
            this.stashItem.setPrefixID(affix.getAffixID());
        }
    }

    public void setSuffix(final DBAffix affix) {
        this.dbSuffix = affix;
        if (affix == null) {
            this.stashItem.setSuffixID(null);
        } else {
            this.stashItem.setSuffixID(affix.getAffixID());
        }
    }

    public void setModifier(final DBAffix affix) {
        this.dbModifier = affix;
        if (affix == null) {
            this.stashItem.setModifierID(null);
        } else {
            this.stashItem.setModifierID(affix.getAffixID());
        }
    }

    public void setCompletionBonus(final DBAffix affix) {
        this.dbBonus = affix;
        if (affix == null) {
            this.stashItem.setCompletionBonusID(null);
        } else {
            this.stashItem.setCompletionBonusID(affix.getAffixID());
        }
    }

    public void setSeedHex(final String s) {
        if (s.length() != 8) {
            this.createSeed();
            return;
        }
        final byte[] bytes = new byte[4];
        for (int i = 0; i < s.length(); i += 2) {
            final char c1 = s.charAt(i);
            final char c2 = s.charAt(i + 1);
            final byte b1 = hexDigitToByte(c1);
            final byte b2 = hexDigitToByte(c2);
            final byte b3 = (byte) (b1 * 16 + b2);
            bytes[bytes.length - 1 - i / 2] = b3;
        }
        this.stashItem.setItemSeed(GDReader.getInt(bytes, 0));
    }

    public void setVar1(final int var1) {
        if (this.stashItem == null) {
            return;
        }
        int temp = 0;
        if (var1 > 0) {
            temp = var1;
        }
        this.stashItem.setVar1(temp);
    }

    public void setVar1Str(final String var1) {
        if (this.stashItem == null) {
            return;
        }
        int i = 0;
        try {
            i = Integer.parseInt(var1);
        } catch (NumberFormatException ex) {
            i = 0;
        }
        this.setVar1(i);
    }

    public void setStackCount(final int stackCount) {
        if (this.stashItem == null) {
            return;
        }
        this.stashItem.setStackCount(stackCount);
    }

    public void setStackCountStr(final String stackCount) {
        if (this.stashItem == null) {
            return;
        }
        int i = 0;
        try {
            i = Integer.parseInt(stackCount);
        } catch (NumberFormatException ex) {
            i = 0;
        }
        this.setStackCount(i);
    }

    public void setX(final int x) {
        this.x = x;
        this.xPos = (float) x;
    }

    private void readOldFormat(final GDLog log) {
        int len = 0;
        try {
            while (len != -1) {
                len = GDReader.readUInt(this.reader);
                if (len != -1) {
                    final String s = GDReader.readString(this.reader, len);
                    if (s.equals("stackCount")) {
                        this.stashItem.setStackCount(GDReader.readInt(this.reader));
                    }
                    if (s.equals("begin_block")) {
                        this.beginBlock = GDReader.readBytes4(this.reader);
                    }
                    if (s.equals("baseName")) {
                        len = GDReader.readUInt(this.reader);
                        if (len > 0) {
                            this.stashItem.setItemID(GDReader.readString(this.reader, len));
                        }
                    }
                    if (s.equals("prefixName")) {
                        len = GDReader.readUInt(this.reader);
                        if (len > 0) {
                            this.stashItem.setPrefixID(GDReader.readString(this.reader, len));
                        }
                    }
                    if (s.equals("suffixName")) {
                        len = GDReader.readUInt(this.reader);
                        if (len > 0) {
                            this.stashItem.setSuffixID(GDReader.readString(this.reader, len));
                        }
                    }
                    if (s.equals("modifierName")) {
                        len = GDReader.readUInt(this.reader);
                        if (len > 0) {
                            this.stashItem.setModifierID(GDReader.readString(this.reader, len));
                        }
                    }
                    if (s.equals("relicName")) {
                        len = GDReader.readUInt(this.reader);
                        if (len > 0) {
                            this.stashItem.setComponentID(GDReader.readString(this.reader, len));
                        }
                    }
                    if (s.equals("relicBonus")) {
                        len = GDReader.readUInt(this.reader);
                        if (len > 0) {
                            this.stashItem.setCompletionBonusID(GDReader.readString(this.reader, len));
                        }
                    }
                    if (s.equals("seed")) {
                        this.stashItem.setItemSeed(GDReader.readInt(this.reader));
                        if (this.stashItem.getItemSeed() == 0) {
                            final Object[] args = {this.filename, this.stashItem.getItemID()};
                            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_NO_SEED", args);
                            GDMsgLogger.addWarning(msg);
                        }
                    }
                    if (s.equals("relicSeed")) {
                        this.stashItem.setComponentSeed(GDReader.readInt(this.reader));
                    }
                    if (s.equals("var1")) {
                        this.stashItem.setVar1(GDReader.readUInt(this.reader));
                    }
                    if (s.equals("velocity")) {
                        final int seed = GDReader.readUInt(this.reader);
                        if (this.stashItem.getComponentID() != null && !this.stashItem.getComponentID().isEmpty()) {
                            this.stashItem.setComponentSeed(seed);
                        }
                        GDReader.readUInt(this.reader);
                        GDReader.readUInt(this.reader);
                    }
                    if (s.equals("owner")) {
                        GDReader.readUInt(this.reader);
                    }
                    if (s.equals("end_block")) {
                        this.endBlock = GDReader.readBytes4(this.reader);
                    }
                    if (s.equals("xOffset")) {
                        this.xPos = GDReader.readFloat(this.reader);
                        this.x = (int) this.xPos;
                    }
                    if (s.equals("yOffset")) {
                        this.yPos = GDReader.readFloat(this.reader);
                        this.y = (int) this.yPos;
                        break;
                    }
                    continue;
                }
            }
            this.stashItem.fillDependentStats(null);
        } catch (IOException ex) {
            if (this.stashItem.getItemID() != null) {
                final Object[] args = {this.filename, this.stashItem.getItemID()};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_ITEM", args);
                log.addError(msg);
            } else {
                final Object[] args = {this.filename};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_ITEM_UNKNOWN", args);
                log.addError(msg);
            }
            this.error = true;
        }
    }

    public void setY(final int y) {
        this.y = y;
        this.yPos = (float) y;
    }

    public void read(final int containerVersion) {
        final GDLog log = new GDLog();
        this.readNewFormat(containerVersion, log);
        this.init();
        this.error = log.containsErrors();
        GDMsgLogger.addLog(log);
        if (!this.error) {
            this.loadData(0);
        }
    }

    public void writeOldFormat(final FileOutputStream writer, final Charset cs) throws IOException {
        writer.write(GDWriter.lengthToBytes4("stackCount"));
        writer.write("stackCount".getBytes(cs));
        writer.write(GDWriter.intToBytes4(this.stashItem.getStackCount()));
        writer.write(GDWriter.lengthToBytes4("begin_block"));
        writer.write("begin_block".getBytes(cs));
        writer.write(this.beginBlock);
        writer.write(GDWriter.lengthToBytes4("baseName"));
        writer.write("baseName".getBytes(cs));
        writer.write(GDWriter.lengthToBytes4(this.stashItem.getItemID()));
        if (this.stashItem.getItemID() != null) {
            writer.write(this.stashItem.getItemID().getBytes(cs));
        }
        writer.write(GDWriter.lengthToBytes4("prefixName"));
        writer.write("prefixName".getBytes(cs));
        writer.write(GDWriter.lengthToBytes4(this.stashItem.getPrefixID()));
        if (this.stashItem.getPrefixID() != null) {
            writer.write(this.stashItem.getPrefixID().getBytes(cs));
        }
        writer.write(GDWriter.lengthToBytes4("suffixName"));
        writer.write("suffixName".getBytes(cs));
        writer.write(GDWriter.lengthToBytes4(this.stashItem.getSuffixID()));
        if (this.stashItem.getSuffixID() != null) {
            writer.write(this.stashItem.getSuffixID().getBytes(cs));
        }
        writer.write(GDWriter.lengthToBytes4("modifierName"));
        writer.write("modifierName".getBytes(cs));
        writer.write(GDWriter.lengthToBytes4(this.stashItem.getModifierID()));
        if (this.stashItem.getModifierID() != null) {
            writer.write(this.stashItem.getModifierID().getBytes(cs));
        }
        writer.write(GDWriter.lengthToBytes4("relicName"));
        writer.write("relicName".getBytes(cs));
        writer.write(GDWriter.lengthToBytes4(this.stashItem.getComponentID()));
        if (this.stashItem.getComponentID() != null) {
            writer.write(this.stashItem.getComponentID().getBytes(cs));
        }
        writer.write(GDWriter.lengthToBytes4("relicBonus"));
        writer.write("relicBonus".getBytes(cs));
        writer.write(GDWriter.lengthToBytes4(this.stashItem.getCompletionBonusID()));
        if (this.stashItem.getCompletionBonusID() != null) {
            writer.write(this.stashItem.getCompletionBonusID().getBytes(cs));
        }
        writer.write(GDWriter.lengthToBytes4("seed"));
        writer.write("seed".getBytes(cs));
        writer.write(GDWriter.intToBytes4(this.stashItem.getItemSeed()));
        writer.write(GDWriter.lengthToBytes4("relicSeed"));
        writer.write("relicSeed".getBytes(cs));
        writer.write(GDWriter.intToBytes4(this.stashItem.getComponentSeed()));
        writer.write(GDWriter.lengthToBytes4("var1"));
        writer.write("var1".getBytes(cs));
        writer.write(GDWriter.intToBytes4(this.stashItem.getVar1()));
        writer.write(GDWriter.lengthToBytes4("end_block"));
        writer.write("end_block".getBytes(cs));
        writer.write(this.endBlock);
        writer.write(GDWriter.lengthToBytes4("xOffset"));
        writer.write("xOffset".getBytes(cs));
        writer.write(GDWriter.floatToBytes4(this.xPos));
        writer.write(GDWriter.lengthToBytes4("yOffset"));
        writer.write("yOffset".getBytes(cs));
        writer.write(GDWriter.floatToBytes4(this.yPos));
    }

    public void refresh() {
        if (this.stashItem != null) {
            final GDLog log = new GDLog();
            this.stashItem.fillDependentStats(log);
            this.loadData(0);
            this.error = log.containsMessages();
            if (this.error) {
                GDMsgLogger.addLog(log);
            }
        }
    }

    private String parseName() {
        final String name = this.dbItem.getItemName();
        return name;
    }

    private ItemInfo compactBonuses() {
        if (this.dbItem == null) {
            return null;
        }
        final ItemInfo info = new ItemInfo();
        List<DBStat> itemStats = this.dbItem.getStatList();
        if (itemStats != null) {
            if (this.isComponent()) {
                int var1 = this.getVar1();
                if (var1 == 0) {
                    var1 = 1;
                }
                final List<DBStat> levelStats = itemStats = DBStat.getStatsForLevel(itemStats, var1);
            }
            DBStat.add(info.itemStats, this.dbItem, 1, itemStats);
        }
        if (this.dbItem.getSkillBonusList() != null) {
            DBSkillBonus.add(info.itemSkillBonuses, this.dbItem.getSkillBonusList());
        }
        if (this.dbItem.getSkillModifierList() != null) {
            info.skillModifiers.addAll(this.dbItem.getSkillModifierList());
        }
        if (this.dbItem.getItemSkill() != null) {
            info.itemSkills.add(new ItemInfo.Skill(this.dbItem.getItemSkill(), this.dbItem.getItemSkillLevel(), this.dbItem.getItemSkillController()));
        }
        if (this.dbItem.getPetBonusSkill() != null) {
            info.petSkills.add(new ItemInfo.Skill(this.dbItem.getPetBonusSkill(), this.dbItem.getItemSkillLevel()));
        }
        if (this.dbPrefix != null) {
            if (this.dbPrefix.getStatList() != null) {
                DBStat.add(info.itemStats, this.dbPrefix, 3, this.dbPrefix.getStatList(this.dbItem.getItemScalePercent()));
            }
            if (this.dbPrefix.getSkillBonusList() != null) {
                DBSkillBonus.add(info.itemSkillBonuses, this.dbPrefix.getSkillBonusList());
            }
            if (this.dbPrefix.getSkillModifierList() != null) {
                info.skillModifiers.addAll(this.dbPrefix.getSkillModifierList());
            }
            if (this.dbPrefix.getItemSkill() != null) {
                info.itemSkills.add(new ItemInfo.Skill(this.dbPrefix.getItemSkill(), this.dbPrefix.getItemSkillLevel(), this.dbPrefix.getItemSkillController()));
            }
            final DBAffix petAffix = this.dbPrefix.getPetAffix();
            if (petAffix != null && petAffix.getStatList() != null) {
                DBStat.add(info.petStats, petAffix, 4, petAffix.getStatList());
            }
            final DBSkill petSkill = this.dbPrefix.getPetSkill();
            if (petSkill != null) {
                final List<DBStat> stats = DBStat.getByLevel(petSkill.getStatList(), 1);
                if (stats != null && !stats.isEmpty()) {
                    DBStat.add(info.petStats, this.dbPrefix, 4, stats);
                }
            }
        }
        if (this.dbSuffix != null) {
            if (this.dbSuffix.getStatList() != null) {
                DBStat.add(info.itemStats, this.dbSuffix, 5, this.dbSuffix.getStatList(this.dbItem.getItemScalePercent()));
            }
            if (this.dbSuffix.getSkillBonusList() != null) {
                DBSkillBonus.add(info.itemSkillBonuses, this.dbSuffix.getSkillBonusList());
            }
            if (this.dbSuffix.getSkillModifierList() != null) {
                info.skillModifiers.addAll(this.dbSuffix.getSkillModifierList());
            }
            if (this.dbSuffix.getItemSkill() != null) {
                info.itemSkills.add(new ItemInfo.Skill(this.dbSuffix.getItemSkill(), this.dbSuffix.getItemSkillLevel(), this.dbSuffix.getItemSkillController()));
            }
            final DBAffix petAffix = this.dbSuffix.getPetAffix();
            if (petAffix != null && petAffix.getStatList() != null) {
                DBStat.add(info.petStats, petAffix, 6, petAffix.getStatList());
            }
            final DBSkill petSkill = this.dbSuffix.getPetSkill();
            if (petSkill != null) {
                final List<DBStat> stats = DBStat.getByLevel(petSkill.getStatList(), 1);
                if (stats != null && !stats.isEmpty()) {
                    DBStat.add(info.petStats, this.dbSuffix, 6, stats);
                }
            }
        }
        if (this.dbModifier != null) {
            if (this.dbModifier.getStatList() != null) {
                DBStat.add(info.itemStats, this.dbModifier, 7, this.dbModifier.getStatList());
            }
            if (this.dbModifier.getSkillBonusList() != null) {
                DBSkillBonus.add(info.itemSkillBonuses, this.dbModifier.getSkillBonusList());
            }
            if (this.dbModifier.getItemSkill() != null) {
                info.itemSkills.add(new ItemInfo.Skill(this.dbModifier.getItemSkill(), this.dbModifier.getItemSkillLevel(), this.dbModifier.getItemSkillController()));
            }
        }
        if (this.dbComponent != null) {
            if (this.dbComponent.getStatList() != null) {
                int var1 = this.getVar1();
                if (var1 == 0) {
                    var1 = 1;
                }
                final List<DBStat> levelStats = DBStat.getStatsForLevel(this.dbComponent.getStatList(), var1);
                DBStat.add(info.componentStats, this.dbComponent, 1, levelStats);
            }
            if (this.dbComponent.getSkillBonusList() != null) {
                DBSkillBonus.add(info.componentSkillBonuses, this.dbComponent.getSkillBonusList());
            }
            if (this.dbComponent.getItemSkill() != null) {
                info.componentSkills.add(new ItemInfo.Skill(this.dbComponent.getItemSkill(), this.dbComponent.getItemLevel(), this.dbComponent.getItemSkillController()));
            }
        }
        if (this.dbBonus != null) {
            if (this.dbBonus.getStatList() != null) {
                DBStat.add(info.completionStats, this.dbBonus, 8, this.dbBonus.getStatList());
            }
            if (this.dbBonus.getSkillBonusList() != null) {
                DBSkillBonus.add(info.completionSkillBonuses, this.dbBonus.getSkillBonusList());
            }
            if (this.dbBonus.getItemSkill() != null) {
                info.completionSkills.add(new ItemInfo.Skill(this.dbBonus.getItemSkill(), this.dbBonus.getItemSkillLevel(), this.dbBonus.getItemSkillController()));
            }
        }
        if (this.dbEnchantment != null && this.dbEnchantment.getStatList() != null) {
            DBStat.add(info.enchantmentStats, this.dbEnchantment, 9, this.dbEnchantment.getStatList());
        }
        return info;
    }

    private String determineCategoryTag() {
        if (this.dbItem == null) {
            return null;
        }
        String tag = null;
        if (this.dbItem.getArmorClass() != null) {
            if (this.dbItem.getArmorClass().equals("Heavy")) {
                tag = "CATEGORY_ARMOR_HEAVY";
            }
            if (this.dbItem.getArmorClass().equals("Caster")) {
                tag = "CATEGORY_ARMOR_CASTER";
            }
        }
        if (this.dbItem.is1HWeapon()) {
            tag = "CATEGORY_WEAPON_1H";
        }
        if (this.dbItem.is2HWeapon()) {
            tag = "CATEGORY_WEAPON_2H";
        }
        return tag;
    }

    private void readNewFormat(final int containerVersion, final GDLog log) {
        switch (containerVersion) {
            case 3: {
                this.readNewFormat_V3(log);
                break;
            }
            case 4: {
                this.readNewFormat_V4(4, log);
                break;
            }
            case 5: {
                this.readNewFormat_V4(4, log);
                break;
            }
            case 6: {
                this.readNewFormat_V4(4, log);
                break;
            }
            case 8: {
                this.readNewFormat_V4(8, log);
                break;
            }
            default: {
                this.error = true;
                break;
            }
        }
    }

    public String determineRarityTag() {
        if (this.dbItem == null) {
            return null;
        }
        String tag = null;
        if (this.dbItem.isArtifact()) {
            if (this.dbItem.getArtifactClass() != null) {
                if (this.dbItem.getArtifactClass().equals("Lesser")) {
                    tag = "CATEGORY_ARTIFACT_EMPOWERED";
                }
                if (this.dbItem.getArtifactClass().equals("Greater")) {
                    tag = "CATEGORY_ARTIFACT_TRANSCENDED";
                }
                if (this.dbItem.getArtifactClass().equals("Divine")) {
                    tag = "CATEGORY_ARTIFACT_MYTHICAL";
                }
            }
        } else {
            final String rarity = this.getRarity();
            if (rarity != null) {
                if (rarity.equals("Magical")) {
                    tag = "CATEGORY_RARITY_MAGICAL";
                }
                if (rarity.equals("Rare")) {
                    tag = "CATEGORY_RARITY_RARE";
                }
                if (rarity.equals("Epic")) {
                    tag = "CATEGORY_RARITY_EPIC";
                }
                if (rarity.equals("Legendary")) {
                    tag = "CATEGORY_RARITY_LEGENDARY";
                }
            }
        }
        return tag;
    }

    private void readNewFormat_V3(final GDLog log) {
        try {
            this.stashItem.readNewFormat(3);
            this.xPos = GDReader.readEncFloat(true);
            this.x = (int) this.xPos;
            this.yPos = GDReader.readEncFloat(true);
            this.y = (int) this.yPos;
        } catch (IOException ex) {
            if (this.stashItem.getItemID() != null) {
                final Object[] args = {this.filename, this.stashItem.getItemID()};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_ITEM", args);
                log.addError(msg);
            } else {
                final Object[] args = {this.filename};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_ITEM_UNKNOWN", args);
                log.addError(msg);
            }
            this.error = true;
        }
    }

    private void readNewFormat_V4(final int containerVersion, final GDLog log) {
        try {
            this.stashItem.readNewFormat(containerVersion);
            if (this.containerType == 1 || this.containerType == 2 || this.containerType == 3) {
                this.xPos = GDReader.readEncFloat(true);
                this.x = (int) this.xPos;
                this.yPos = GDReader.readEncFloat(true);
                this.y = (int) this.yPos;
            }
            if (this.containerType == 4) {
                this.x = GDReader.readEncInt(true);
                this.xPos = (float) this.x;
                this.y = GDReader.readEncInt(true);
                this.yPos = (float) this.y;
            }
            if (this.containerType == 6) {
                this.attached = GDReader.readEncByte();
            }
        } catch (IOException ex) {
            if (this.stashItem.getItemID() != null) {
                final Object[] args = {this.filename, this.stashItem.getItemID()};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_ITEM", args);
                log.addError(msg);
            } else {
                final Object[] args = {this.filename};
                final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_ITEM_UNKNOWN", args);
                log.addError(msg);
            }
            this.error = true;
        }
    }

    public int getByteSize(final int containerVersion) {
        int size = this.stashItem.getByteSize(containerVersion);
        if (this.containerType == 1 || this.containerType == 2 || this.containerType == 3) {
            size += 4;
            size += 4;
        }
        if (this.containerType == 4 || this.containerType == 5) {
            size += 4;
            size += 4;
        }
        if (this.containerType == 6) {
            ++size;
        }
        return size;
    }

    public void write(final int containerVersion) throws IOException {
        this.stashItem.write(containerVersion);
        if (this.containerType == 1 || this.containerType == 2 || this.containerType == 3) {
            GDWriter.writeFloat(this.xPos);
            GDWriter.writeFloat(this.yPos);
        }
        if (this.containerType == 4 || this.containerType == 5) {
            GDWriter.writeInt(this.x);
            GDWriter.writeInt(this.y);
        }
        if (this.containerType == 6) {
            GDWriter.writeByte(this.attached);
        }
    }

    public void createSeed() {
        this.stashItem.setItemSeed(generateSeed());
    }

    public String getItemSetBonuses() {
        if (this.dbItem == null) {
            return null;
        }
        String s = this.dbItem.getItemSetName();
        if (s == null) {
            return null;
        }
        s = GDColor.HTML_COLOR_ITEMSET + s + "</font>" + "<br>";
        final ArrayList<List<DBStat>> arrStat = this.dbItem.getItemSetBonusesPerLevel();
        final ArrayList<List<DBSkillBonus>> arrBonus = this.dbItem.getItemSetSkillBonusesPerLevel();
        for (int i = 0; i < arrStat.size(); ++i) {
            final Object[] args = {i + 1};
            final String title = GDMsgFormatter.format(GDMsgFormatter.rbGD, "TXT_SET_NUM_ITEMS", args);
            boolean hasTitle = false;
            String txtStat = null;
            if (!arrStat.get(i).isEmpty() || !arrBonus.get(i).isEmpty()) {
                final DetailComposer comp = DetailComposer.createComposer(this.stashItem, 1, arrStat.get(i), arrBonus.get(i));
                txtStat = DetailComposer.getComposerBonuses(comp, true, true, false);
                if (txtStat != null) {
                    if (!hasTitle) {
                        s += title;
                        hasTitle = true;
                    }
                    s = s + "<br>" + GDColor.HTML_COLOR_STAT + txtStat + "</font>";
                }
            }
            final List<DBSkillModifier> modifiers = this.dbItem.getSkillModifiers(i);
            if (modifiers != null && !modifiers.isEmpty()) {
                final DetailComposer compSkillModifier = DetailComposer.createComposerSkillMod(this.stashItem, 1, modifiers);
                final String txtMod = DetailComposer.getComposerBonuses(compSkillModifier, true, true, false);
                if (txtMod != null) {
                    if (!hasTitle) {
                        s += title;
                        hasTitle = true;
                    }
                    s = s + "<br>" + GDColor.HTML_COLOR_STAT + txtMod + "</font>";
                }
            }
        }
        return s;
    }

    private String determineItemTypeTag() {
        if (this.dbItem == null) {
            return null;
        }
        if (this.dbItem.getItemClass() == null) {
            return null;
        }
        String tag = null;
        if (this.dbItem.getItemClass().equals("ArmorProtective_Head")) {
            tag = "TYPE_ARMOR_HEAD";
        }
        if (this.dbItem.getItemClass().equals("ArmorProtective_Shoulders")) {
            tag = "TYPE_ARMOR_SHOULDERS";
        }
        if (this.dbItem.getItemClass().equals("ArmorProtective_Chest")) {
            tag = "TYPE_ARMOR_CHEST";
        }
        if (this.dbItem.getItemClass().equals("ArmorProtective_Hands")) {
            tag = "TYPE_ARMOR_HANDS";
        }
        if (this.dbItem.getItemClass().equals("ArmorProtective_Legs")) {
            tag = "TYPE_ARMOR_LEGS";
        }
        if (this.dbItem.getItemClass().equals("ArmorProtective_Feet")) {
            tag = "TYPE_ARMOR_FEET";
        }
        if (this.dbItem.getItemClass().equals("ArmorProtective_Waist")) {
            tag = "TYPE_ARMOR_BELT";
        }
        if (this.dbItem.getItemClass().equals("ArmorJewelry_Amulet")) {
            tag = "TYPE_ARMOR_AMULET";
        }
        if (this.dbItem.getItemClass().equals("ArmorJewelry_Medal")) {
            tag = "TYPE_ARMOR_MEDAL";
        }
        if (this.dbItem.getItemClass().equals("ArmorJewelry_Ring")) {
            tag = "TYPE_ARMOR_RING";
        }
        if (this.dbItem.getItemClass().equals("ItemRelic")) {
            tag = "TYPE_ITEM_COMPONENT";
        }
        if (this.dbItem.getItemClass().equals("ItemArtifact")) {
            tag = "TYPE_ITEM_ARTIFACT";
        }
        if (this.dbItem.getItemClass().equals("ItemEnchantment")) {
            tag = "TYPE_ITEM_ENCHANTMENT";
        }
        if (this.dbItem.getItemClass().equals("WeaponArmor_Offhand")) {
            tag = "TYPE_WEAPON_OFFHAND";
        }
        if (this.dbItem.getItemClass().equals("WeaponArmor_Shield")) {
            tag = "TYPE_WEAPON_SHIELD";
        }
        if (this.dbItem.getItemClass().equals("WeaponMelee_Axe")) {
            tag = "TYPE_WEAPON_AXE";
        }
        if (this.dbItem.getItemClass().equals("WeaponMelee_Mace")) {
            tag = "TYPE_WEAPON_MACE";
        }
        if (this.dbItem.getItemClass().equals("WeaponHunting_Spear")) {
            tag = "TYPE_WEAPON_SPEAR";
        }
        if (this.dbItem.getItemClass().equals("WeaponMelee_Sword")) {
            tag = "TYPE_WEAPON_SWORD";
        }
        if (this.dbItem.getItemClass().equals("WeaponMelee_Dagger")) {
            tag = "TYPE_WEAPON_DAGGER";
        }
        if (this.dbItem.getItemClass().equals("WeaponMelee_Scepter")) {
            tag = "TYPE_WEAPON_SCEPTER";
        }
        if (this.dbItem.getItemClass().equals("WeaponHunting_Ranged1h")) {
            tag = "TYPE_WEAPON_RANGED";
        }
        if (this.dbItem.getItemClass().equals("WeaponMelee_Axe2h")) {
            tag = "TYPE_WEAPON_AXE";
        }
        if (this.dbItem.getItemClass().equals("WeaponMelee_Mace2h")) {
            tag = "TYPE_WEAPON_MACE";
        }
        if (this.dbItem.getItemClass().equals("WeaponMelee_Spear2h")) {
            tag = "TYPE_WEAPON_SPEAR";
        }
        if (this.dbItem.getItemClass().equals("WeaponMagical_Staff")) {
            tag = "TYPE_WEAPON_STAFF";
        }
        if (this.dbItem.getItemClass().equals("WeaponMelee_Sword2h")) {
            tag = "TYPE_WEAPON_SWORD";
        }
        if (this.dbItem.getItemClass().equals("WeaponHunting_Ranged2h")) {
            tag = "TYPE_WEAPON_RANGED";
        }
        if (this.dbItem.getItemClass().equals("ItemNote")) {
            tag = "TYPE_ITEM_NOTE";
        }
        return tag;
    }

    public static class LabelInfo {
        public String text;
        public Color foreground;
        public Color background;

        public LabelInfo() {
            this.foreground = GDColor.COLOR_FG_COMMON;
            this.background = GDColor.COLOR_BG_COMMON;
            this.text = "";
        }
    }
}
